package eppic.pkg;

import java.text.SimpleDateFormat;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.content.Context;

public class Log {
	
	public int id;
	public int parent_id;
	public int is_project;
	public long start;
	public long end;
	
	public Log()
	{
		this.id = 0;
		this.parent_id = 0;
		this.is_project = 0;
		this.start = System.currentTimeMillis();
		this.end = System.currentTimeMillis();
		
	}
	
	public Log(int id, int parent_id, int is_project, long start, long end)
	{
		this.id = id;
		this.parent_id = parent_id;
		this.is_project = is_project;
		this.start = start;
		this.end = end;
		
	}
	
	public Log(int parent_id, int is_project, long start, long end)
	{
		this.parent_id = parent_id;
		this.is_project = is_project;
		this.start = start;
		this.end = end;
		
	}
	
	public Log(int id, Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		
        Cursor c=db.rawQuery("SELECT * FROM logs WHERE _id="+id, null);
        c.moveToFirst();
       
    	this.id = c.getInt(c.getColumnIndex("_id"));
    	this.parent_id = c.getInt(c.getColumnIndex("parent_id"));
    	this.is_project = c.getInt(c.getColumnIndex("is_project"));
    	this.start = c.getLong(c.getColumnIndex("start"));
    	this.end = c.getLong(c.getColumnIndex("end"));


        c.close();
        db.close();
	}
	
	public String date()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");
		String date = sdf.format(this.start);
		
		return date;
		
	}
	
	public String startTime()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
		String date = sdf.format(this.start);
		
		return date;
	}
	
	public String endTime()
	{
		SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
		String date = sdf.format(this.end);
		
		return date;
	}
	
	public float time()
	{
		return (this.end - this.start);
	}
	
	public String timeWorked()
	{
		float diff = this.end - this.start;
	    int minutes = (int) ((diff / 1000) / 60);
	    int hours = minutes / 60;
	    minutes = minutes % 60;
		
		return String.format("%d:%02d", hours, minutes);
	}
	
	public boolean delete(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        try{
        	db.execSQL("DELETE FROM logs WHERE _id="+this.id);
        }catch(Exception e)
        {
        	return false;
        }
        
		return true;
	}
	
	
	public boolean save(Context context)
	{
		DatabaseHelper dbHelper = new DatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        
        //Values
		cv.put("parent_id", this.parent_id);
		cv.put("is_project", this.is_project);
		cv.put("start", this.start);
		cv.put("end", this.end);
		
		if(db.insert("logs", "parent_id", cv) != -1)
		{
			db.close();
			return true;
		}
		else
		{
			db.close();
			return false;
		}
	
	}
	
}
	
	

